/**
 * 初始化路由
 * author: LeiHao
 * createTime: 2023/12/28
 */

// 4.0.14
import { createWebHistory, createRouter } from "vue-router";
import useAppStore from "@/store/modules/app";

// 静态路由
export const constantRoutes = [{
    path: "/",
    name: 'home',
    component: () => import("@/views/Index.vue"),
    hidden: true,
    meta: {
        title: '地图'
    }
}, {
    path: "/government",
    name: 'governmentMap',
    component: () => import("@/views/government/Index.vue"),
    hidden: true,
    title: '地图 - 土地推介平台',
    meta: {
        title: '一地一档'
    }
}, {
    path: "/promotion",
    name: 'promotionMap',
    component: () => import("@/views/promotion/Index.vue"),
    hidden: true,
    title: '地图 - 土地推介平台',
    meta: {
        title: 'AI智选'
    }
}];

const router = createRouter({
    history: createWebHistory('/map'),
    routes: constantRoutes,
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition;
        } else {
            return {
                top: 0
            };
        }
    }
});

export const resetRouter = () => {
    router.matcher = createRouter({
        history: createWebHistory('/map'),
        routes: constantRoutes
    }).matcher;
};

// vue-router 全局前置守卫
router.beforeEach(async (to, from, next) => {
    // 设置网页 title
    to.meta.title && useAppStore().setTitle(to.meta.title);

    next();
});

export default router;